//
//  RegisterViewController.m
//  DialectShare
//
//  Created by timmy zhong on 3/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()
@property (nonatomic, retain) UserProxy *proxy;
@end

@implementation RegisterViewController
@synthesize account = _account, name = _name, password =_password, password2 = _password2;
@synthesize proxy = _proxy;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    self.name = nil;
    self.password2 = nil;
    self.password = nil;
    self.account = nil;
    self.proxy = nil;
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"注册";
    
    _proxy = [[UserProxy alloc] init];
    _proxy.delegate = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)cancel:(id)sender
{
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)submit:(id)sender
{
    NSString *name  = self.name.text;
    NSString *account = self.account.text;
    NSString *password = self.password.text;
    NSString *password2 = self.password2.text;
    NSString *msg = @"";
    BOOL show = NO;
    if (!show && [account isEqualToString:@""]) {
            
        msg = @"帐号不能为空";
        show = YES;
    }
    if (!show && [password isEqualToString:@""]) {
        msg = @"密码不能为空";
        show = YES;
    }
    if (!show && ![password isEqualToString:password2]) {
        msg = @"密码不匹配";
        show = YES;
    }    
    if (!show && [name isEqualToString:@""]) {
        msg = @"名称不能为空";
        show = YES;
    }
    if (show) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:msg delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        return ;
    }
    
    User *user = [[User alloc] init];
    user.account = account;
    user.name = name;
    user.password = password;
    [_proxy registerUser:user];
    [user release];
    
}

- (void)requestFailWithProxy:(Proxy *)proxy
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:(NSString *)proxy.object delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}
- (void)requestFinishWithProxy:(Proxy *)proxy
{
    NSLog(@"done");
    [self dismissModalViewControllerAnimated:YES];
    [self dismissModalViewControllerAnimated:NO];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];        
    [UIView setAnimationDuration:0.30f];        
    CGRect rect = self.view.frame;
    rect.origin.y += 80;
    self.view.frame = rect;        
    [UIView commitAnimations];        
    [textField resignFirstResponder];

    return YES;        
}

- (void)keyboardWillShow:(NSNotification *)noti
{        
    CGRect frame = self.view.frame;        
    if (frame.origin.y < 0) {
        return ;
    }
    frame.origin.y -= 80;
    [UIView beginAnimations:@"Curl"context:nil];//动画开始          
    [UIView setAnimationDuration:0.30f];           
    [UIView setAnimationDelegate:nil];          
    [self.view setFrame:frame];         
    [UIView commitAnimations];         
}



@end
